<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>单选框</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <link rel="stylesheet" href="../formdesign/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="./css/ueditor/site.css">
    <script type="text/javascript" src="../dialogs/internal.js"></script>
    <script type="text/javascript" src="../../../js/jquery/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="../../../js/base/base.js"></script>
    <link rel="stylesheet" href="leipi.style.css">
    <script type="text/javascript">
function createElement(type, name)
{     
    var element = null;
    try {        
        element = document.createElement('<'+type+' name="'+name+'">');     
    } catch (e) {}   
    if(element==null) {     
        element = document.createElement(type);     
        element.name = name;     
    } 
    return element;     
}
function fnSelect( combo ) {
    var iIndex = combo.selectedIndex ;
    oListText.selectedIndex    = iIndex ;
    var olistText    = document.getElementById( "orgtext" ) ;
    olistText.value    = oListText.value ;
}

function fnAdd() {
    var olistText    = document.getElementById( "orgtext" ) ;
    fnAddComboOption( oListText, olistText.value, olistText.value ) ;
    oListText.selectedIndex = oListText.options.length - 1 ;
    olistText.value    = '' ;
    olistText.focus() ;
}

function fnModify() {
    var iIndex = oListText.selectedIndex ;
    if ( iIndex < 0 ) return ;
    var olistText    = document.getElementById( "orgtext" ) ;
    oListText.options[ iIndex ].innerHTML    = fnHTMLEncode( olistText.value ) ;
    oListText.options[ iIndex ].value        = olistText.value ;
    olistText.value    = '' ;
    olistText.focus() ;
}

function fnMove( steps ) {
    fnChangeOptionPosition( oListText, steps ) ;
}

function fnDelete() {
    fnRemoveSelectedOptions( oListText ) ;
}

function fnSetSelectedValue() {
    var iIndex = oListText.selectedIndex ;
    if ( iIndex < 0 ) return ;
    var olistText = document.getElementById( "orgvalue" ) ;
    olistText.innerHTML = oListText.options[ iIndex ].value ;
}

// Moves the selected option by a number of steps (also negative)
function fnChangeOptionPosition( combo, steps ) {
    var iActualIndex = combo.selectedIndex ;
    if ( iActualIndex < 0 ){
        return ;
    }
    var iFinalIndex = iActualIndex + steps ;
    if ( iFinalIndex < 0 ){
        iFinalIndex = 0 ;
    }
    if ( iFinalIndex > ( combo.options.length - 1 ) ) {
        iFinalIndex = combo.options.length - 1 ;
    }
    if ( iActualIndex == iFinalIndex ) {
        return ;
    }
    var oOption = combo.options[ iActualIndex ] ;
    if(oOption.value=="") {
        var sText    = fnHTMLDecode( oOption.value ) ;
    } else {
        var sText    = fnHTMLDecode( oOption.innerHTML ) ;
    }
    combo.remove( iActualIndex ) ;
    oOption = fnAddComboOption( combo, sText, sText, null, iFinalIndex ) ;
    oOption.selected = true ;
}

// Remove all selected options from a SELECT object
function fnRemoveSelectedOptions( combo ) {
    // Save the selected index
    var iSelectedIndex = combo.selectedIndex ;
    var oOptions = combo.options ;
    // Remove all selected options
    for ( var i = oOptions.length - 1 ; i >= 0 ; i-- ) {
        if (oOptions[i].selected) combo.remove(i) ;
    }

    // Reset the selection based on the original selected index
    if ( combo.options.length > 0 ) {
        if ( iSelectedIndex >= combo.options.length ) iSelectedIndex = combo.options.length - 1 ;
        combo.selectedIndex = iSelectedIndex ;
    }
}

// Add a new option to a SELECT object (combo or list)
function fnAddComboOption( combo, optionText, optionValue, documentObject, index ) {
    var oOption ;
    if ( documentObject ) {
        oOption = documentObject.createElement("option") ;
    } else {
        oOption = document.createElement("option") ;
    }
    if ( index != null ) {
        combo.options.add( oOption, index ) ;
    } else {
        combo.options.add( oOption ) ;
    }
    oOption.innerHTML = optionText.length > 0 ? fnHTMLEncode( optionText ) : '&nbsp;' ;
    oOption.value     = optionValue ;
    return oOption ;
}

function fnHTMLEncode( text ) {
    if ( !text ) {
        return '' ;
    }
    text = text.replace( /&/g, '&amp;' ) ;
    text = text.replace( /</g, '&lt;' ) ;
    text = text.replace( />/g, '&gt;' ) ;
    return text ;
}


function fnHTMLDecode( text ) {
    if ( !text ) {
        return '' ;
    }
    text = text.replace( /&gt;/g, '>' ) ;
    text = text.replace( /&lt;/g, '<' ) ;
    text = text.replace( /&amp;/g, '&' ) ;
    return text ;
}

function fnSetAttribute( element, attName, attValue ) {
    if ( attValue == null || attValue.length == 0 ){
        element.removeAttribute( attName, 0 ) ;        
    } else {
        element.setAttribute( attName, attValue, 0 ) ;    
    }
}
    </script>
</head>
<body>
<div class="content">
    <table class="table table-bordered table-striped table-hover">
    <tr>
        <th><span>控件名称</span><span class="label label-important">*</span></th>
        <th><span>初始选定</span></th>
    </tr>
    <tr>
        <td> <input id="orgname" type="text" placeholder="必填项" /> </td></td><input type="hidden" id="fielid" placeholder="必填项">
        <td> <span id="orgvalue" class="uneditable-input"></span> </td>
    </tr>
    <tr>
        <th colspan="2">
            <span>单选框选项</span><span class="label label-important">*</span>
        </th>
    </tr>
    <tr>
        <td colspan="2">
            <select id="orglist" multiple="multiple" class="span14"></select>
        </td>
    </tr>
    <tr>
        <td>
            <div class="btn-group pull-right">
                <a title="新增" onclick="fnAdd();" class="btn btn-primary"><i class="icon-white icon-plus"></i></a>
                <a title="修改" onclick="fnModify();" class="btn btn-default"><i class="icon-edit"></i></a>
            </div>
            <input type="text" placeholder="输入列表值..." class="span2" id="orgtext">
        </td>
        <td>
            <div class="btn-group">
                <button title="上移" onclick="fnMove(-1);" class="btn btn-default"><i class="icon-arrow-up"></i></button>
                <button title="下移" onclick="fnMove(1);" class="btn btn-default"><i class="icon-arrow-down"></i></button>
                <button title="设为初始化时选定值" onclick="fnSetSelectedValue();" class="btn btn-default"><i class="icon-ok-circle"></i></button>
                <button title="删除" onclick="fnDelete();" class="btn btn-default"><i class="icon-ban-circle"></i></button>
            </div>
        </td>
    </tr>
    </table>
    <div class="alert alert-danger">提示：需要预览才能看到实际效果</div>
</div>
<script type="text/javascript">
    var oNode = null,oListText='',thePlugins = 'radio';
    oListText    = $G( 'orglist' ) ;
    $(function () {
        var dataid = $.getQueryString('dataid');
        var dataNumber=parent.pluginId;
     if( UE.plugins[thePlugins].editdom ){
            oNode = UE.plugins[thePlugins].editdom;
            var gTitle=oNode.getAttribute('title').replace(/&quot;/g,"\""),gRadios=oNode.getAttribute('radio_field'),gChecked=oNode.getAttribute("orgchecked");
            gTitle = gTitle==null ? '' : gTitle;
            $G('orgvalue').innerHTML = gChecked;
            $G('orgname').value = gTitle;

            var sDataField = gRadios;
            var aDataField = sDataField.split('`');
            var sLvCheck = gChecked;
            // Load the actual options
            for ( var i = 0 ; i < aDataField.length ; i++ ) {
                var sText    = aDataField[i] ;
                if(aDataField[i] == "") {
                    continue;
                }
                fnAddComboOption( oListText, sText, sText ) ;
            }
        }

    dialog.oncancel = function () {
        if( UE.plugins[thePlugins].editdom ) {
            delete UE.plugins[thePlugins].editdom;
        }
    };
    dialog.onok = function (){
        if($G('orgname').value == '') {
            alert('控件名称不能为空');
            return false;
        }
        if( oListText.options.length == 0 ) {
            alert('请添加选项！');
            return false;
        }
        var gValue=$G('orgvalue').innerHTML,gTitle=$G('orgname').value.replace(/\"/g,"&quot;"),gid = $G('fielid').value;;


        var sDataField = '',sLvCheck = '';
        for ( var i = 0 ; i < oListText.options.length ; i++ ) {
            var sText    = oListText.options[i].value ;
            if ( sText.length == 0 ) sText = sText ;

            if ( sText == gValue ) {
                sLvCheck = sText;
            }
            if( sDataField==sText+"`" && sText != "`") {
                alert("选项 ["+sText+"] 重复");
                return false;
            }
            sDataField += sText+"`";
        }
        if( !oNode ) {
            try {
                oNode = createElement('input','leipiNewField');
                oNode.setAttribute('title',gTitle);
                oNode.setAttribute('type','radio');
                oNode.setAttribute('checked','checked');
                oNode.setAttribute('radio_field',sDataField);
                oNode.setAttribute('orgchecked',sLvCheck);
                oNode.setAttribute('classname',thePlugins);
                 oNode.setAttribute('class', "form_item");
                oNode.setAttribute('data-type', "radio");
                oNode.setAttribute('name', 'DATA_' + dataNumber);
                oNode.setAttribute('id', 'DATA_' + dataNumber);
                editor.execCommand('insertHtml',oNode.outerHTML);
                return true ;
            } catch ( e ) {
                try {
                    editor.execCommand('error');
                } catch ( e ) {
                    alert(控件异常);
                }
                return false;
            }
        } else {
            oNode.setAttribute('class', "form_item");
            oNode.setAttribute('data-type', "radio");
            oNode.setAttribute('title',gTitle);
            oNode.setAttribute('radio_field',sDataField);
            oNode.setAttribute('orgchecked',sLvCheck);
            oNode.setAttribute('name', gid);
            oNode.setAttribute('id', gid);
            delete UE.plugins[thePlugins].editdom;
            return true;
        }
    };

});


</script>
</body>
</html>